package fr.inserm.umr915.vcfannotator.bio;

public class GeneticCode
	{
	private String ncbiTable;
	/** the standard genetic code */
	private static final String STANDARD="FFLLSSSSYY**CC*WLLLLPPPPHHQQRRRRIIIMTTTTNNKKSSRRVVVVAAAADDEEGGGG";

	public GeneticCode()
		{	
		this(STANDARD);
		}
	
	public GeneticCode(String ncbiTable)
		{	
		this.ncbiTable=ncbiTable;
		}
		
	/** get the genetic-code table (NCBI data) */ 
	protected String getNCBITable()
		{
		return ncbiTable;
		}
	
	/** convert a base to index */
	private static int base2index(char c)
		{
		switch(c)
			{
			case 'T': return 0;
			case 'C': return 1;
			case 'A': return 2;
			case 'G': return 3;
			default: return -1;
			}
		}
	/** translate cDNA to aminoacid */
	public char translate(char b1,char b2,char b3)
		{
		int base1= base2index(b1);
		int base2= base2index(b2);
		int base3= base2index(b3);
		if(base1==-1 || base2==-1 || base3==-1)
			{
			return '?';
			}
		else
			{
			return getNCBITable().charAt(base1*16+base2*4+base3);
			}
		}
	}
